package com.pk.platform.business.common.service.impl;

import java.util.List;
import java.util.Set;

import com.pk.platform.business.common.dao.IKindergartenDao;
import com.pk.platform.business.common.service.IKindergartenService;
import com.pk.platform.business.common.vo.KinderVO;
import com.pk.platform.domain.common.Kindergarten;
import com.pk.platform.domain.privilege.User;
import com.pk.platform.util.StringConverter;
import com.pk.platform.util.constant.Constant;
import com.pk.platform.util.page.ListPage;
import com.pk.platform.util.page.Pager;

public class KindergartenServiceImpl implements IKindergartenService {

	private IKindergartenDao kindergartenDao;
	
	public ListPage queryKindergarten(KinderVO kvo, Pager pager) {
		return kindergartenDao.queryKindergarten(kvo, pager);
	}
	
	public void addKindergarten(Kindergarten kindergarten)
	{
		kindergartenDao.add(kindergarten);
		kindergartenDao.addOperateLog("新增幼儿园", Constant.SUCCESSFUL, kindergarten);
	}
	
	public void updateKindergarten(Kindergarten kindergarten)
	{
		Kindergarten kg = this.queryKindergartenById(kindergarten.getId());
		kg.setKgName(kindergarten.getKgName());
		kg.setKgCode(kindergarten.getKgCode());
		kg.setAddress(kindergarten.getAddress());
		kg.setTelephone(kindergarten.getTelephone());
		kg.setCreateTime(kindergarten.getCreateTime());
		kindergartenDao.addOperateLog("修改幼儿园", Constant.SUCCESSFUL, kg);
	}
	
	public String delKindergarten(String id)
	{
		String result = "";
		Kindergarten kinder = this.queryKindergartenById(id);
		kindergartenDao.addOperateLog("删除幼儿园", Constant.SUCCESSFUL, kinder);
		Set<User> userSet = kinder.getUserSet();
		for(User user : userSet)
		{
			user.setKindergarten(null);
			String kgDataPrivilege = user.getKgDataPrivilege();
			kgDataPrivilege = StringConverter.removeValue(kgDataPrivilege, "'" + id + "'", ",");
			user.setKgDataPrivilege(kgDataPrivilege);
		}
		kinder.setIsDel(1);
		return result;
	}
	
	public Kindergarten queryKindergartenById(String id)
	{
		return kindergartenDao.get(Kindergarten.class, id);
	}
	
	public List<Kindergarten> queryAllKgList()
	{
		return kindergartenDao.queryAllKgList();
	}
	
	
	

	public IKindergartenDao getKindergartenDao() {
		return kindergartenDao;
	}

	public void setKindergartenDao(IKindergartenDao kindergartenDao) {
		this.kindergartenDao = kindergartenDao;
	}

}
